package com.gitee.fanx.sample.otpauth.dao;

import com.gitee.fanx.sample.otpauth.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author fanhang
 */
@Component
public class UserStore {
    private Map<String, User> userStore = new HashMap<>();

    public void put(String phone, User user) {
        userStore.put(phone, user);
    }

    public User get(String phone) {
        return userStore.get(phone);
    }

    public List<User> all() {
        return userStore.values().stream().map(i -> cloneUser(i)).collect(Collectors.toList());
    }

    private User cloneUser(User user) {
        User newUser = new User();
        BeanUtils.copyProperties(user, newUser);
        return newUser;
    }
}
